package com.cmc.cloud.cmclink.doc.controller.admin;

import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.convert.PortDescConvert;
import com.cmc.cloud.cmclink.doc.entity.PortDescDO;
import com.cmc.cloud.cmclink.doc.service.PortDescService;
import com.cmc.cloud.cmclink.doc.vo.portdesc.*;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;


/**
 * 港口描述前端接口Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - 港口描述")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/port-desc")
public class PortDescController {

    @Resource
    private PortDescService portDescService;

    @Operation(summary = "创建港口描述")
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission('doc:maintainB/LPortDescription:add')")
    public CommonResult<Void> createPortDesc(@Valid @RequestBody PortDescCreateReqVO createReqVO) {
        portDescService.createPortDesc(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新港口描述")
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermission({'doc:maintainB/LPortDescription:edit','doc:maintainB/LPortDescription:enable'})")
    public CommonResult<Void> updatePortDesc(@Valid @RequestBody PortDescUpdateReqVO updateReqVO) {
        portDescService.updatePortDesc(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除港口描述")
    @PostMapping("/delete/{id}")
    @PreAuthorize("@ss.hasPermission('doc:maintainB/LPortDescription:delete')")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Void> deletePortDesc(@PathVariable("id") Long id) {
        portDescService.deletePortDesc(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得港口描述")
    @GetMapping("/get/{id}")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<PortDescRespVO> getPortDesc(@PathVariable("id") Long id) {
        PortDescDO portDesc = portDescService.getPortDesc(id);
        return CommonResult.success(PortDescConvert.INSTANCE.convert(portDesc));
    }

    @Operation(summary = "校验港口是否存在默认描述是true,否false")
    @GetMapping("/check/{portCode}/{id}")
    public CommonResult<Boolean> checkPortIsDefault(@PathVariable("portCode") String portCode, @PathVariable("id") Long id) {
        return CommonResult.success(portDescService.checkPortIsDefault(portCode, id));
    }

    @Operation(summary = "获得港口描述列表")
    @GetMapping("/list")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<PortDescRespVO>> getPortDescList(@RequestParam("ids") Collection<Long> ids) {
        List<PortDescDO> list = portDescService.getPortDescList(ids);
        return CommonResult.success(PortDescConvert.INSTANCE.convertList(list));
    }

    @Operation(summary = "获得港口描述分页")
    @PostMapping("/page")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission('doc:maintainB/LPortDescription:search')")
    public CommonResult<PageResult<PortDescRespVO>> getPortDescPage(@Valid @RequestBody PortDescPageReqVO pageVO) {
        PageResult<PortDescDO> pageResult = portDescService.getPortDescPage(pageVO);
        List<PortDescRespVO> results = PortDescConvert.INSTANCE.convertList(pageResult.getList());
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }

    @Operation(summary = "获得港口描述")
    @PostMapping("/info")
    @PreAuthorize("@ss.hasPermission('doc:maintainB/LPortDescription:search')")
    public CommonResult<List<PortDescInfoRespVO>> getPortDescInfo(@RequestBody List<String> port) {
        return CommonResult.success(portDescService.getPortDescInfo(port));
    }
}
